package com.mypage.view.activity;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.litesuits.android.log.Log;
import com.mypage.model.PublicTwo;

public class SecondLogin extends Activity{
	//倒计时
	public TextView textView;
	//按钮
	public Button login,unlogin,btnsecond;
	public ImageView imageView;
	//三个传值
	public String hostname,uuid,time;
	//判断是否是login.cloudcc.com
	public boolean islogin = false;
	int ceshi = 30;
	public TextView topLine;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.secondlogin);
		login = (Button) findViewById(R.id.btnBtn);
		unlogin = (Button) findViewById(R.id.btnCancel);
		btnsecond=(Button) findViewById(R.id.btnSecond);
		topLine= (TextView) findViewById(R.id.topLine);
		imageView =  (ImageView) findViewById(R.id.backuser);
		hostname = getIntent().getStringExtra("hostname");
		uuid =  getIntent().getStringExtra("uuid");
		time =  getIntent().getStringExtra("time");
		ceshi = Integer.valueOf(time);
		if(ceshi>0){
			initTime();
		}else{
			login.setEnabled(false);
			unlogin.setEnabled(false);
		}
		addlistener();
		
		
	}
	private void initTime() {
		
			handler.postDelayed(runnable, 1000);   
	}
	 Handler handler = new Handler();    
	    Runnable runnable = new Runnable() {    
	        @Override    
	        public void run() {    
	        	ceshi--;
//	        	topLine.setText("二维码有效时间" + ceshi);
	        	if(ceshi==0){
//	        		topLine.setText(getResources().getString(R.string.saoshixiao));
					topLine.setVisibility(View.VISIBLE);
	        		topLine.setTextColor(Color.RED);
					topLine.setText(R.string.guoqierweima);
	        		login.setVisibility(View.GONE);
	        		unlogin.setVisibility(View.GONE);
	        		btnsecond.setVisibility(View.VISIBLE);
	        	}else{
	            handler.postDelayed(this, 1000);    
	        	}    
	        }
	    };

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		String url = "";
		RequestParams params = new RequestParams();
		if(UrlManager.getInterfaceYm().contains(hostname)){
			url = UrlManager.getInterfaceUrl();

			islogin = false;
		}
		else if("120.27.162.206:9004".equals(hostname)){
			url = "http://120.27.162.206:9004/distributor.action";
			//				else if("login.cloudcc.com".equals(hostname)){
			//					url = "http://login.cloudcc.com";
			//					http://120.27.162.206:9004/
			islogin = true;
		}else{
			//					Toast.makeText(SecondLogin.this, getResources().getString(R.string.saoqushibai), 1).show();
		}
		//				/distributor.action?serviceName=scaned&uuid=xxxxx
		params.addBodyParameter("serviceName", "cancelLogin");
		params.addBodyParameter("uuid", uuid);
		webquxiao(params, url);
		return super.onKeyDown(keyCode, event);
	}
	private void addlistener() {
		btnsecond.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.putExtra("replaySao","true");
				setResult(-2,intent);
				finish();
//				startActivity(intent);
			}
		});
		imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String url = "";
				RequestParams params = new RequestParams();
				if(UrlManager.getInterfaceYm().contains(hostname)){
					url = UrlManager.getInterfaceUrl();

					islogin = false;
				}
				else if("120.27.162.206:9004".equals(hostname)){
					url = "http://120.27.162.206:9004/distributor.action";
					//				else if("login.cloudcc.com".equals(hostname)){
					//					url = "http://login.cloudcc.com";
					//					http://120.27.162.206:9004/
					islogin = true;
				}else{
					//					Toast.makeText(SecondLogin.this, getResources().getString(R.string.saoqushibai), 1).show();
					return;
				}

				//				/distributor.action?serviceName=scaned&uuid=xxxxx
				params.addBodyParameter("serviceName", "cancelLogin");
				params.addBodyParameter("uuid", uuid);
				webquxiao(params, url);
			}
		});
		login.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String url = UrlManager.getInterfaceUrl();
				RequestParams params = new RequestParams();
				if(UrlManager.getInterfaceYm().contains(hostname)){
					params.addBodyParameter("uuid", uuid);
					islogin = false;
				}else if("120.27.162.206:9004".equals(hostname)){
//					url = "http://120.27.162.206:9004/distributor.action";
//				else if("login.cloudcc.com".equals(hostname)){
//					url = "http://login.cloudcc.com";   
//					http://120.27.162.206:9004/
					islogin = true;
				}else{
//					Toast.makeText(SecondLogin.this, getResources().getString(R.string.saoshibai), 1).show();
					return;
				}
				params.addBodyParameter("serviceName", "confirmAppLogin");
				params.addBodyParameter("binding", RunTimeManager.getInstance()
						.getServerBinding());
				web(params, url);
			}
		});
		
		unlogin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String url = "";
				RequestParams params = new RequestParams();
				if(UrlManager.getInterfaceYm().contains(hostname)){
					url = UrlManager.getInterfaceUrl();
					
					islogin = false;
				}
				else if("120.27.162.206:9004".equals(hostname)){
					url = "http://120.27.162.206:9004/distributor.action";
//				else if("login.cloudcc.com".equals(hostname)){
//					url = "http://login.cloudcc.com"; 
//					http://120.27.162.206:9004/
					islogin = true;
				}else{
//					Toast.makeText(SecondLogin.this, getResources().getString(R.string.saoqushibai), 1).show();
					return;
				}
				
//				/distributor.action?serviceName=scaned&uuid=xxxxx
				params.addBodyParameter("serviceName", "cancelLogin");
				params.addBodyParameter("uuid", uuid);
				webquxiao(params, url);
			}
		});
	}
	public void web(RequestParams params,String url){
		Log.d("params", params.toString());
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {
	
					@Override
					public void onFailure(HttpException arg0, String arg1) {
						Log.d("-------------------------", "Failure。。。"
								+ arg1.toString());
						Toast.makeText(SecondLogin.this, getResources().getString(R.string.saounqingqiu), 1).show();
						finish();
					}
					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						Log.d("-------------------------", "success。。。"
							+ arg0.result);	
						Gson gson = new Gson();
						PublicTwo publicTwo = gson.fromJson(arg0.result, PublicTwo.class);
						if("true".equals(publicTwo.result)){
							if(islogin){
								RequestParams params = new RequestParams();
								params.addBodyParameter("serviceName", "confirmLogin");
								params.addBodyParameter("uuid", uuid);
								params.addBodyParameter("randomCode",publicTwo.data.randomCode);
								params.addBodyParameter("appUrl",UrlManager.getInterfaceSao().substring(7));
								params.addBodyParameter("loginName", publicTwo.data.userName);
								Log.d("canshu", uuid+"::"+publicTwo.data.randomCode+"::"+UrlManager.getInterfaceSao().substring(7)+"::"+publicTwo.data.userName);
//								String url = "http://login.cloudcc.com/distributor.acton";
								
								String url = "http://120.27.162.206:9004/distributor.action";
								Log.d("request", url+"?serviceName=confirmLogin&uuid="+uuid+"&randomCode="+publicTwo.data.randomCode+"&appUrl="+UrlManager.getInterfaceSao().substring(7)+"&loginName="+publicTwo.data.userName);
								islogin = false;
								web(params, url);
							}else if("2".equals(publicTwo.returnCode)){
//								Toast.makeText(SecondLogin.this, getResources().getString(R.string.saologin), 1).show();
								finish();
							}
						}else if("3".equals(publicTwo.returnCode)){
//							Toast.makeText(SecondLogin.this, "超时", 1).show();
//							Toast.makeText(SecondLogin.this,publicTwo.returnInfo , 1).show();
							topLine.setText(getResources().getString(R.string.saoshixiao));
							topLine.setTextColor(Color.RED);
							topLine.setVisibility(View.VISIBLE);
			        		login.setVisibility(View.GONE);
			        		unlogin.setVisibility(View.GONE);
			        		btnsecond.setVisibility(View.VISIBLE);
			        		
						}else{
							Toast.makeText(SecondLogin.this, publicTwo.returnInfo, 1).show();
							finish();
						}
						}
				});
	}
	public void webquxiao(RequestParams params,String url){
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {
					@Override
					public void onFailure(HttpException arg0, String arg1) {
						Log.d("-------------------------", "Failure。。。"
								+ arg1.toString());
						Toast.makeText(SecondLogin.this, getResources().getString(R.string.saounqingqiu), 1).show();
						finish();
					}
					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						Log.d("quxiaodenglu", "success。。。"
							+ arg0.result+"::"+arg0.toString());
						try {
							Gson gson = new Gson();
							PublicTwo publicTwo = gson.fromJson(arg0.result, PublicTwo.class);
							if("true".equals(publicTwo.result)){
								
//								Toast.makeText(SecondLogin.this, getResources().getString(R.string.saounlogin), 1).show();
								finish();
						}else{
							Toast.makeText(SecondLogin.this, publicTwo.returnInfo, 1).show();
							finish();
						}
						} catch (Exception e) {
							Toast.makeText(SecondLogin.this, "请求失败", 1).show();
						}
						
						
						}
				});
	}
}
